package com.yc.rh.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.yc.rh.dao.ICommentsInfoDao;
import com.yc.rh.entities.CommentsInfo;
import com.yc.rh.entities.NewartInfo;
import com.yc.rh.entities.UserInfo;

public class CommentsInfoDaoImpl implements ICommentsInfoDao{
	private SqlSession sqlSession;
	
	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}
	
	@Override
	public int addCommentsInfo(UserInfo uf, CommentsInfo cf) {
		Map<String,Object> map=new HashMap<String,Object>();
		cf.setText1(String.valueOf(getTotal(cf.getNaid())+1));
		map.put("userInfo", uf);
		map.put("commentsInfo", cf);
		return sqlSession.insert("addComment", map);
	}

	@Override
	public List<CommentsInfo> find(Integer pageNo, Integer pageSize, int naid) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("min", (pageNo-1)*pageSize);
		map.put("max", pageNo*pageSize);
		map.put("naid", naid);
		return sqlSession.selectList("getCommentAll", map);
	}

	@Override
	public int delComments(String csids) {
		List<Integer> list=new ArrayList<Integer>();
		while(csids.contains(",")){
			Integer csid=Integer.valueOf(csids.substring(0, csids.indexOf(",")));
			csids=csids.substring(csids.indexOf(",")+1);
			list.add(csid);
		}
		list.add(Integer.valueOf(csids));
		return sqlSession.delete("delComments",list);
	}

	@Override
	public int updateComments(CommentsInfo cf) {
		return 0;
	}

	@Override
	public int getTotal(int naid) {
		CommentsInfo ci=new CommentsInfo();
		ci.setNaid(naid);
		return sqlSession.selectOne("getCommentTotal", ci);
	}
}
